package com.example.tang.zhayan.fragment;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.alibaba.fastjson.JSON;
import com.example.tang.zhayan.R;
import com.example.tang.zhayan.adapter.LimitBodyAdapter;
import com.example.tang.zhayan.bean.BaseLimit;
import com.example.tang.zhayan.bean.LimitItem;
import com.example.tang.zhayan.constant.PathConstant;
import com.example.tang.zhayan.ui.BuyDetailsActivity;
import com.example.tang.zhayan.utils.PopupwindowUtils;
import com.example.tang.zhayan.utils.ResultParamsUtils;
import com.example.tang.zhayan.utils.WindowsUtils;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.xutils.common.Callback;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by tang on 2015/12/14.
 */

@ContentView(R.layout.fragment_limit_time)
public class LimitTimeFragment extends BaseFragment {
    @ViewInject(R.id.ptr_limit_time)
    private PullToRefreshListView ptrListView;
    @ViewInject(R.id.img_limit_search)
    private ImageView imgSearch;
    private Map<String, String> map;
    private int pageNo = 1;
    private List<LimitItem> itemList;
    private LimitBodyAdapter adapter;
    private PopupWindow popupSearchWindow;

    /**
     * 单例模式
     *
     * @return
     */
    public static LimitTimeFragment newInstance() {
        LimitTimeFragment fragment = new LimitTimeFragment();
        return fragment;
    }

    /**
     * 初始化控件 或者 数据
     * 比 initData() 方法先执行
     */
    @Override
    protected void initView() {
        super.initView();
        map = new HashMap<>();
        itemList = new ArrayList<>();
        adapter = new LimitBodyAdapter(getActivity(), itemList);
        ptrListView.setMode(PullToRefreshListView.Mode.BOTH);
        ptrListView.setAdapter(adapter);
        addListener();
    }


    /**
     * 滑动监听事件
     */
    private void addListener() {
        //搜索点击事件
        imgSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View view = LayoutInflater.from(context).inflate(R.layout.activity_search, null);
                popupSearchWindow = new PopupWindow(view, new WindowsUtils(context)
                        .getScreenWidth(), new WindowsUtils(context).getScreenHeight());
                PopupwindowUtils.getSearchPopupWindow(v, view, getActivity(), popupSearchWindow);
            }
        });

        //ListView的点击事件
        ptrListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), BuyDetailsActivity.class);
                intent.putExtra("id", itemList.get(position - 1).getId());
                intent.putExtra("memberId", itemList.get(position - 1).getStylist_id());
                startActivity(intent);
            }
        });

        ptrListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                downLoadData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                pageNo++;
                downLoadData();
            }
        });

    }


    /**
     * 初始化控件 或者 数据
     */
    @Override
    protected void initData() {
        super.initData();
        downLoadData();
    }

    /**
     * 下载数据
     */
    private void downLoadData() {
        map.put("pageSize", "10");
        map.put("pageNo", pageNo + "");
        x.http().post(ResultParamsUtils.getIDParams(PathConstant.LIMIT_BODY, map), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String s) {
                BaseLimit baseLimit = JSON.parseObject(s, BaseLimit.class);
                itemList.addAll(baseLimit.getData().getList());
                adapter.notifyDataSetChanged();
                ptrListView.onRefreshComplete();
            }

            @Override
            public void onError(Throwable throwable, boolean b) {

            }

            @Override
            public void onCancelled(CancelledException e) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
